﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
//using Modbus.Device;
//using Modbus.Utility;
using MySql.Data.MySqlClient;

namespace TJ_MainNameSpace
{
    public partial class For_shebei : Form
    {
        public For_shebei()
        {
            InitializeComponent();
        }
        DataSet ds1 = new DataSet();

        //MySqlConnection con = new MySqlConnection(new MySQLConnectionString(TongJi_EMS.Properties.Settings.Default["server"].ToString(), TongJi_EMS.Properties.Settings.Default["database"].ToString(), TongJi_EMS.Properties.Settings.Default["mysqluser"].ToString(), TongJi_EMS.Properties.Settings.Default["mysqlpassword"].ToString(), shujuyuan3.port).AsString);
        MySqlConnection con = new MySqlConnection(TongJi_EMS.Properties.Settings.Default.monitorConnection);

        private System.Windows.Forms.SaveFileDialog saveFileDialog1 = new SaveFileDialog();
        private OpenFileDialog openfile = new OpenFileDialog();
        public static bool ifalart = false;
        private void For_shebei_Load(object sender, EventArgs e)
        {
            try
            {
                fresh();
            }
            catch (Exception exp)
            {
                MessageBox.Show(exp.ToString());
                this.Close();
            }
        }

        public void fresh()
        {
            con.Open();
            //老代码
            //string sql = "select slaverid,name,protocol,description,id from device where project='" + FormStart.Gongcheng_Name + "'";
            //数据库重新设计后的最新更改
            string sql = "select slaver_address,name,protocol,description,id from device where project='" + TongJi_EMS._Management.ProjectManager.Instance.CurProjectName + "'";
            MySqlDataAdapter oda = new MySqlDataAdapter(sql, con);
            DataSet ds1 = new DataSet();
            ds1.Clear();
            oda.Fill(ds1, "device");
            this.dataGridView1.DataSource = ds1.Tables["device"];

            this.comboBox1.DataSource = ds1.Tables["device"];
            this.comboBox1.DisplayMember = "name";
            this.comboBox1.ValueMember = "id";
            con.Close();
        }

        private void treeView1_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            switch (e.Node.Text)
            {
                case "Modbus(ASCII串行口)":
                    Peizhi1 a = new Peizhi1();
                    a.Show();
                    Peizhi1.protocol = "Modbus(ASCII串行口)";
                    break;
                case "Modbus(RTU串行口)":
                    Peizhi1 b = new Peizhi1();
                    b.Show();
                    Peizhi1.protocol = "Modbus(RTU串行口)";
                    break;
                case "OPC":
                    //  OPC opc=new OPC ();
                    //opc.Show();
                    Peizhi1.protocol = "OPC";
                    break;
                default:
                    //MessageBox.Show("请选择一个节点");
                    break;
            }
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            string item = "";
            try
            {
                item = comboBox1.Text;
            }
            catch (Exception)
            {
                MessageBox.Show("没有设备可供删除！");
            }
            //老代码
            //string mysql = "delete from device where id= '" + item + "'AND project='" + FormStart.Gongcheng_Name + "'";
            //数据库重新设计后的最新更改
            string mysql = "delete from device where name= '" + item + "'";
            MySqlCommand mInsertCmd = new MySqlCommand(mysql, con);

            if (MessageBox.Show("删除该设备将导致与此设备相关的所有变量点均被删除！", "警告", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) == DialogResult.OK)
            {
                con.Open();
                mInsertCmd.ExecuteNonQuery();
                con.Close();
            }
            fresh();
        }

        private void For_shebei_Activated(object sender, EventArgs e)
        {
            fresh();
        }

        private void buttonalert_Click(object sender, EventArgs e)
        {
            string item = "";
            ifalart = true;

            con.Open();
            try
            {
                item = comboBox1.Text;
            }
            catch (Exception)
            {
                MessageBox.Show("没有设备可供修改！");
            }
            //老代码
            //string mysql = "select slaverid,name from device where id = '" + item + "'AND project='" + FormStart.Gongcheng_Name + "'";
            //数据库重新设计后的最新更改
            string mysql = "select slaver_address,area_name,COM, BaudRate,DataBits,Parity,StopBits from device where name = '" + item + "'";
            MySqlCommand mInsertCmd = new MySqlCommand(mysql, con);

            MySqlDataReader reader = mInsertCmd.ExecuteReader();
            while (reader.Read())
            {
                Peizhi1.address = reader.GetValue(0).ToString();
                Peizhi1.name = item;
                Peizhi1.new_name = item;
                //数据库重新设计后的最新更改
                Peizhi1.area_name = reader.GetValue(1).ToString();
                Peizhi2.port = reader.GetValue(2).ToString();
                Peizhi2.baudrate = reader.GetValue(3).ToString();
                Peizhi2.databit = reader.GetValue(4).ToString();
                Peizhi2.parity = reader.GetValue(5).ToString();
                Peizhi2.stopbit = reader.GetValue(6).ToString();
            }
            Peizhi1 a = new Peizhi1();
            a.Show();
            con.Close();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            saveFileDialog1.Filter = "XML File|*.xml";
            saveFileDialog1.Title = "Save XML File";
            saveFileDialog1.InitialDirectory = TongJi_EMS._Management.ProjectManager.Instance.CurProjectPath;
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                string fimename1 = saveFileDialog1.FileName;
                DataTable dt = new DataTable();
                dt = (DataTable)dataGridView1.DataSource;
                dt.WriteXml(fimename1);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            openfile.Filter = "XML File|*.xml"; ;
            openfile.Title = "Save XML File";
            openfile.InitialDirectory = TongJi_EMS._Management.ProjectManager.Instance.CurProjectPath;
            if (openfile.ShowDialog() == DialogResult.OK)
            {
                string filepath = openfile.FileName;
                DataSet ds = new DataSet();
                ds.ReadXml(filepath);
                this.dataGridView1.DataSource = ds.Tables[0];
                this.comboBox1.DataSource = ds.Tables[0];
                this.comboBox1.DisplayMember = "name";
                this.comboBox1.ValueMember = "id";
            }
        }
    }
}